package com.nsn.auction.domina;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Type;

@Entity
@Table(name="AUCTION")
public class Auction implements Serializable{

	private Long auctionId;
	private Item item;
	private Price price;
	private Date startDate;
	private Date endDate;
	private boolean finished;
	private Set<Offer> offers;
	private User owner;
	private Category category;

	@Temporal(TemporalType.DATE)
	@Column(name="END_DATE", unique = false, nullable = false, length = 20)
	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	
	@Type(type="yes_no")
	@Column(name="IS_FINISHED", nullable = false, length = 1)
	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public Price getPrice() {
		return price;
	}

	public void setPrice(Price price) {
		this.price = price;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "AUCTION_ID", unique = true, nullable = false)
	public Long getAuctionId() {
		return auctionId;
	}

	public void setAuctionId(Long auctionId) {
		this.auctionId = auctionId;
	}

	@Temporal(TemporalType.DATE)
	@Column(name="START_DATE", unique = false, nullable = false, length = 20)
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	@OneToMany(fetch = FetchType.LAZY)
	public Set<Offer> getOffers() {
		return offers;
	}

	public void setOffers(Set<Offer> offers) {
		this.offers = offers;
	}

	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
	
	@Override
	public boolean equals(Object object) {
		
		//check for selt-comparison
		if (this == object) return true;
		if (!( this.getClass().equals(object.getClass()) )) return false;
		
		Auction temp_auction = (Auction) object;
		 return (null == getOwner() ? null == temp_auction.getOwner() : getAuctionId().equals(temp_auction.getOwner()) &&
				null == getItem() ? null == temp_auction.getItem() : getAuctionId().equals(temp_auction.getItem()) &&
				null == getStartDate() ? null == temp_auction.getStartDate() : getAuctionId().equals(temp_auction.getStartDate()) &&
				false == isFinished() ? false == temp_auction.isFinished() : getAuctionId().equals(temp_auction.isFinished()) &&
				null == getEndDate() ? null == temp_auction.getEndDate() : getAuctionId().equals(temp_auction.getEndDate()));
	}
	
}
